package main

import (
	"fmt"
	"bytes"
	"golang.org/x/crypto/ssh"
)

func main(){
	simpleSSH_example()
}

func simpleSSH_example(){
	fmt.Println("Start to test ssh ...")
	ip,port,username,password,cmd:="10.99.201.175","22","wcadmin","1q2w#E$R5","sleep 3;pwd"
	
	errStr:=""
	
	fmt.Println(ip,port,username,password,cmd)
	
	config:=&ssh.ClientConfig{
		User:username,
		Auth:[]ssh.AuthMethod{ssh.Password(password)},
	}
	client,_:=ssh.Dial("tcp",ip+":"+port,config)
	session,err:=client.NewSession()
	if err!=nil {
		errStr+=err.Error()
	}
	
	defer session.Close()
	var buf bytes.Buffer
	var errBuf bytes.Buffer
	session.Stdout=&buf
	session.Stderr=&errBuf
	session.Run(cmd)
	result:=string(buf.Bytes())
	errStr+=string(errBuf.Bytes())
	
	fmt.Println("Result:")
	fmt.Println(result)
	fmt.Println("Error:")
	fmt.Println(errStr)
	
	fmt.Println("End of ssh test ...")
	
}